/*
 * @(#)ErrorUtil.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Web Platform Development Team.
 */
public class ErrorUtil {
	
	/**
	 * Gets the message and stack trace.
	 * 
	 * @param throwable the throwable
	 * 
	 * @return the message and stack trace
	 */
	public static String getMessageAndStackTrace(Throwable throwable) {
		StackTraceElement[] elements = throwable.getStackTrace();
		StringBuffer sb = new StringBuffer();
		sb.append("Exception Message : ");
		sb.append(throwable.getMessage());
		sb.append("\nStackTrace :\n");
		
		for (StackTraceElement element : elements) {
			sb.append(element.toString());
			sb.append("\n");
		}
		
		return sb.toString();
	}

	// Message로 부터 Properties Map을 생성
	/**
	 * Gets the properties.
	 * 
	 * @param message the message
	 * 
	 * @return the properties
	 */
	public static Map<String, String> getProperties(String message) {
		if (message == null) {
			return Collections.emptyMap();
		}
		
		String[] messages = StringUtil.split(message, "&");
		Map<String, String> properties = new HashMap<String, String>();
		String msg;
		
		for (String str : messages) {
			msg = str.trim();
			int pos = msg.indexOf("=");
			
			if (pos > 0) {
				properties.put(msg.substring(0, pos).trim(), msg.substring(pos + 1).trim());
			}
		}
		
		return properties;
	}
}
